import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Report from '../components/report/Report.vue'
import Allocation from '../components/repair/allocation.vue'
import Strived from '../components/repair/strived.vue'
import Repairing from '../components/repair/repairing.vue'
import Evaluate from '../components/repair/evaluate.vue'
import Errorbill from '../components/repair/errorbill.vue'
import Total from '../components/echart/total.vue'
import User from '../components/user/Users.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/toreport', component:Report },
      { path: '/allocation', component:Allocation },
      { path: '/strived', component:Strived },
      { path: '/repairing', component:Repairing },
      { path: '/evaluate', component:Evaluate },
      { path: '/errorbill', component:Errorbill },
      { path: '/total', component:Total },
      { path: '/user', component:User },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const isToken = sessionStorage.getItem('token')
  if (!isToken) return next('/login')
  next()
})

export default router
